package com.ruoyi.eamp.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eamp.mapper.EampSrManMapper;
import com.ruoyi.eamp.domain.EampSrMan;
import com.ruoyi.eamp.service.IEampSrManService;

/**
 * 库房管理
Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class EampSrManServiceImpl implements IEampSrManService 
{
    @Autowired
    private EampSrManMapper eampSrManMapper;

    /**
     * 查询库房管理

     * 
     * @param srId 库房管理
主键
     * @return 库房管理

     */
    @Override
    public EampSrMan selectEampSrManBySrId(Long srId)
    {
        return eampSrManMapper.selectEampSrManBySrId(srId);
    }

    @Override
    public EampSrMan selectEampSrManBySrName(String srName) {
        return eampSrManMapper.selectEampSrManBySrName(srName);
    }

    /**
     * 查询库房管理
列表
     * 
     * @param eampSrMan 库房管理

     * @return 库房管理

     */
    @Override
    public List<EampSrMan> selectEampSrManList(EampSrMan eampSrMan)
    {
        return eampSrManMapper.selectEampSrManList(eampSrMan);
    }

    /**
     * 新增库房管理

     * 
     * @param eampSrMan 库房管理

     * @return 结果
     */
    @Override
    public int insertEampSrMan(EampSrMan eampSrMan)
    {
        return eampSrManMapper.insertEampSrMan(eampSrMan);
    }

    /**
     * 修改库房管理

     * 
     * @param eampSrMan 库房管理

     * @return 结果
     */
    @Override
    public int updateEampSrMan(EampSrMan eampSrMan)
    {
        LocalDateTime dateTime = LocalDateTime.now();
        eampSrMan.setSrUpdateTime(dateTime);
        Long userId = SecurityUtils.getUserId();
        eampSrMan.setSrUpdateBy(userId);
        return eampSrManMapper.updateEampSrMan(eampSrMan);
    }

    /**
     * 批量删除库房管理

     * 
     * @param srIds 需要删除的库房管理
主键
     * @return 结果
     */
    @Override
    public int deleteEampSrManBySrIds(Long[] srIds)
    {
        return eampSrManMapper.deleteEampSrManBySrIds(srIds);
    }

    /**
     * 删除库房管理
信息
     * 
     * @param srId 库房管理
主键
     * @return 结果
     */
    @Override
    public int deleteEampSrManBySrId(Long srId)
    {
        return eampSrManMapper.deleteEampSrManBySrId(srId);
    }

    @Override
    public List<EampSrMan> selectEampSrManListAll(EampSrMan eampSrMan) {
        return eampSrManMapper.selectEampSrManListAll(eampSrMan);
    }

    @Override
    public int updateAddEampSrManById(Long modifiedWarehouse) {
        int result = eampSrManMapper.updateAddEampSrManById(modifiedWarehouse);
        if(result == 1 ){
            EampSrMan eampSrMan = eampSrManMapper.selectEampSrManBySrId(modifiedWarehouse);
            if (eampSrMan.getSrCurrentSpecifications() == 0) {
                eampSrMan.setSrState(1L);
                eampSrManMapper.updateEampSrMan(eampSrMan); // 确保有这个方法来更新数据库
            }
        }
       return result;
    }

    @Override
    public int updateRemoveEampSrManById(Long originalWarehouse) {
        int result = eampSrManMapper.updateRemoveEampSrManById(originalWarehouse);
        if(result == 1){
            EampSrMan eampSrMan = eampSrManMapper.selectEampSrManBySrId(originalWarehouse);
            // 检查当前规格是否不为 0
            if (eampSrMan.getSrCurrentSpecifications() != 0) {
                // 设置状态为 0
                eampSrMan.setSrState(0L);
                // 更新数据库中的 EampSrMan 对象
                eampSrManMapper.updateEampSrMan(eampSrMan); // 确保有这个方法
            }
        }
        return result;
    }
}
